package org.tsp.android.GUI_Menus;


import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class SlideShowScroll extends SurfaceView implements Callback, Runnable {

	Thread thread = null;
	
	private SurfaceHolder mHolder = null;
	
	private int m_Width;
	private int m_Height;
	
	private Boolean running = false;
	
	private Bitmap m_Bitmap = null;
	private Bitmap m_Bitmap1 = null;
	
	private Date begin = null;
	
	public SlideShowScroll(Context context) {
		super(context);
		mHolder = getHolder();
		mHolder.addCallback(this);
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		m_Width = width;
		m_Height = height;
	}
	
	public void surfaceCreated(SurfaceHolder holder) {
		startThread();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		stopThread();
	}
	
	private synchronized void startThread()
	{
		if ( !running )
		{
			// To complete
			// Start the thread
			this.thread = new Thread(this);
			thread.start();
			running = true;
		}
	}
	
	private synchronized void stopThread()
	{
		if ( running )
		{
			// To complete
			// Stop the thread
			running = false;
			try {
				this.thread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	
	public void run() {
		Canvas c = null;
		while (running)
		{
			c = null;
			try {
			     c = mHolder.lockCanvas();
			     
			     // To complete
	    		 // Clear the screen
			     c.drawColor(Color.BLACK);
			     
			     synchronized (mHolder) {
			    	 if ( m_Bitmap != null )
				     {
			    		 // To complete
			    		 // Draw bitmap on the canvas 
			    		 // or an animation with both bitmaps
			    		 // The animation should last for 500ms
			    		 if(begin == null)
			    		 {
				    		 c.drawBitmap(m_Bitmap, 0, 0,paint);			    			 
			    		 }else
			    		 {
			    			int diff =  (new Date()).compareTo(begin);
			    			diff = diff;
			    			 if(diff  <  500)
			    			 {
			    				 //image => image1
			    				 //image =>
			    				 //draw image
			    			 }else{
			    				 //animation for the scroll
			    			 }
			    			 
			    		 }

				    	  
				     }
			     }
		    } finally {
		    	if ( c!= null )
		    		mHolder.unlockCanvasAndPost(c);
			}
		}
		running = false;
	}
	
	public void setBitmap(Bitmap bmp)
	{	
		if ( !running )
			return;
		float bmp_ar = (float)bmp.getWidth() / (float)bmp.getHeight();
		float scr_ar = (float)m_Width / (float)m_Height;
		float scale = 1;
		
		if ( scr_ar < bmp_ar )
			scale = (float)m_Width/(float)bmp.getWidth();
		else
			scale = (float)m_Height/(float)bmp.getHeight();
			
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);
		
		synchronized (mHolder) {	
			if ( m_Bitmap == null )
				m_Bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
			else
			{
				m_Bitmap1 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
				begin = new Date();
			}
		}
		this.postInvalidate();
	}
	
}
